package com.draven.interceptor;

import com.draven.constants.FramworkConstants;
import com.draven.entity.User;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Created by Draven on 2017/4/11.
 * E-mail:draven1122@163.com
 */
public class SessionValidate implements HandlerInterceptor {
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) throws Exception {
        HttpSession session=request.getSession(true);
        //session中获取用户名信息
        User user = (User)session.getAttribute(FramworkConstants.USER_SESSION_KEY);
        if (user==null) {
            response.sendRedirect(request.getSession().getServletContext().getContextPath()+"/passport/login");
            return false;
        }
        return true;
    }

    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object o, ModelAndView modelAndView) throws Exception {

    }

    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

    }
}
